package cz.muni.fi.pa165.musiclibrary.musiclibraryweb;

import java.util.Locale;

import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.util.convert.ConversionException;
import org.apache.wicket.util.convert.IConverter;
import org.joda.time.Duration;

/**
 *
 * @author Maros Kucbel
 * @date 2012-11-26T22:42:01+0100
 */
public final class DurationConverter implements IConverter<Duration> {

    @Override
    public Duration convertToObject(String value, Locale locale) {
        if (StringUtils.isNumeric(value)) {
            return Duration.standardSeconds(Long.valueOf(value));
        }
        throw new ConversionException(String.format("Can't convert [%1$s] to Duration.", value));
    }

    @Override
    public String convertToString(Duration value, Locale locale) {
        return String.valueOf(value.getStandardSeconds());
    }

}